Promise.myRace = function (promises) {
	// 用来只输出一个结果而设置的标识
	let state = false;
	return new Promise((resolve, reject) => {
		promises.forEach((item) => {
			Promise.resolve(item)
				.then((val) => {
					if (!state) {
						// 有第一个结果且状态为fulfilled
						state = true;
						resolve(val);
					}
				})
				.catch((err) => {
					// 有第一个结果且状态为rejected
					reject(err);
				});
		});
	});
};

// test
let p1 = new Promise((resolve, reject) => {
	setTimeout(() => {
		resolve(111);
	}, 1000);
});

let p2 = new Promise((resolve, reject) => {
	setTimeout(() => {
		reject(222);
	}, 2000);
});

let p3 = new Promise((resolve, reject) => {
	setTimeout(() => {
		resolve(333);
	}, 3000);
});
Promise.myRace([p1, p2, p3])
	.then((res) => {
		console.log(res);
	})
	.catch((err) => {
		console.log(err);
	});
